<!--
 * @Description: 
 * @Autor: Thlq
 * @Date: 2020-12-24 09:23:09
 * @LastEditors: Thlq
 * @LastEditTime: 2020-12-24 17:14:03
-->
<template>
	<view class="container more">
		<image :src="baseImgUrl + '/VKCEYUGU-aa407feb-6c9e-46ce-9252-7d2145b8d504/6695fa32-a6a2-497a-a041-cc37259510fb.png'" class="all-back"></image>
		<view class="wrapper">
			<u-input v-model="pasteValue" type="text" :border="true" placeholder="将复制的地址,粘贴到这里就可以去水印了啊" />
			<view class="btn-wrap">
				<u-button type="primary" size="medium" @click="paste">粘贴连接</u-button>
				<u-button type="success" size="medium" @click="clearWatermark">去除水印</u-button>
			</view>
			<view v-if="showVideo">
				<u-gap height="4" bg-color="#eee"></u-gap>
				<view class="btn-wrap">
					<u-button type="warning" size="medium" @click="save">保存视频</u-button>
					<u-button type="error" size="medium" @click="copy">复制地址</u-button>
				</view>
				<view class="video-wrap">
					<video object-fit="contain" id="myVideo" :src="videoUrl" controls></video>
				</view>
			</view>
			<view class="btn-share">
				<u-button type="info" open-type="share">分享朋友</u-button>
			</view>
			<view class="ad-wraper">
				<ad unit-id="adunit-e52230f6b15b325a"></ad>
			</view>
		</view>
		<view class="pageLoading" v-show="loading">
			<full-loading bgColor="rgba(255, 255, 255, .7)" />
		</view>
	</view>
</template>
<script>
import fullLoading from "@/components/full-loading.vue"
import { baseImgUrl, waterMarkUrl } from "@/config/config.js"
export default {
	components: {
		fullLoading
	},
	data() {
		return {
			loading: false,
			baseImgUrl,
			pasteValue: "",
			videoUrl: "",
			showVideo: false,
			tempFilePath: ""
		}
	},
	onLoad() {
		// this.getList()
	},
	onShareAppMessage: function() {
		return {
			title: "抖音/快手全网短视频免费使用",
			path: "/pages/watermark/index",
			imageUrl: baseImgUrl + "/VKCEYUGU-aa407feb-6c9e-46ce-9252-7d2145b8d504/03252fc7-c579-4bea-91a4-b7cd0e7b3ace.png",
			success: function(e) {}
		};
	},
	onShareTimeline: function() {
		return {
			title: "抖音/快手全网短视频免费使用",
			path: "/pages/watermark/index",
			imageUrl: baseImgUrl + "/VKCEYUGU-aa407feb-6c9e-46ce-9252-7d2145b8d504/03252fc7-c579-4bea-91a4-b7cd0e7b3ace.png",
			success: function(e) {}
		};
	},
	methods: {
		paste() {
			const that = this
			uni.getClipboardData({
				success(e) {
					if (e.errMsg == "getClipboardData:ok") {
						that.pasteValue = e.data
					}
				}
			})
		},
		clearWatermark() {
			const that = this
			const { pasteValue } = this
			if (pasteValue.length <=0) {
				uni.showToast({
					title: '需要粘贴链接地址',
					icon: 'none'
				})
				return
			}
			const index = pasteValue.indexOf('https://')
			if (index < 0) {
				uni.showToast({
					title: '分享地址不太对哦',
					icon: 'none'
				})
				return
			}
			const url = `${waterMarkUrl}${this._parseUrl(pasteValue)}`
			this.loading = true
			uni.request({
				url,
				success(e) {
					that.loading = false
					if (e.statusCode == 200 && e.data.code == 200) {
						that.videoUrl = e.data.data.url
						that.showVideo = true
					} else {
						that.showVideo = false
						uni.showToast({
							title: '不支持您输入的链接',
							icon: 'none'
						})
					}
				}
			})
		},
		copy() {
			const that = this
			uni.setClipboardData({
				data: that.videoUrl,
				showToast: true,
				success() {}
			})
		},
		save() {
			const that = this
			uni.showModal({
				title: "温馨提示",
				content: "您确定要保存视频到您的相册吗",
				success(e) {
					if (e.cancel) {
						return
					}
					that.loading = true
					uni.downloadFile({
						url: that.videoUrl,
						success(res) {
							that.tempFilePath = res.tempFilePath
							uni.authorize({
								scope: "scope.writePhotosAlbum",
								success() {
									uni.saveVideoToPhotosAlbum({
										filePath: that.tempFilePath,
										success() {
											that.loading = false
											uni.showToast({
												title: '保存成功',
												icon: 'none'
											})
										}
									})
								},
								fail() {
									that.loading = false
									uni.showModal({
										title: "温馨提示",
										content: "请前往小程序设置允许添加到相册",
										showCancel: false
									})
								}
							})
						}
					})
				}
			})
		},
		_parseUrl(text) {
		  let startIndex = text.lastIndexOf('http://')
		  startIndex = startIndex === -1 ? text.lastIndexOf('https://') : startIndex
		  if (startIndex === -1) {
		    console.log('请输入正确的视频链接')
		    return
		  }
		  //去掉前面的中文
		  let link = text.substr(startIndex)
		  const endIndex = link.indexOf(' ')
		  if (endIndex !== -1) {
		    link = link.substring(0, endIndex)
		  }
		  return link
		}
	}
}
</script>

<style lang="scss">
@import "./index.scss";
</style>
